{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a4bd5f2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
    "os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "09374178",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fpf-1b-32k/checkpoint-17900'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers.trainer_utils import get_last_checkpoint\n",
    "\n",
    "latest = get_last_checkpoint(\"fpf-1b-32k\")\n",
    "latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6729e33",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7f19fb64",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('mesolitica/llama-7b-hf-32768-fpf', trust_remote_code=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7ee76221",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/cextension.py:34: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.\n",
      "  warn(\"The installed version of bitsandbytes was compiled without GPU support. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cadam32bit_grad_fp32\n",
      "[2023-09-19 12:53:21,419] [INFO] [real_accelerator.py:133:get_accelerator] Setting ds_accelerator to cuda (auto detect)\n"
     ]
    }
   ],
   "source": [
    "model = AutoModelForCausalLM.from_pretrained(latest, torch_dtype=torch.float16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "57a018fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LlamaConfig {\n",
       "  \"_name_or_path\": \"fpf-1b-32k/checkpoint-17900\",\n",
       "  \"architectures\": [\n",
       "    \"LlamaForCausalLM\"\n",
       "  ],\n",
       "  \"bos_token_id\": 1,\n",
       "  \"eos_token_id\": 2,\n",
       "  \"hidden_act\": \"silu\",\n",
       "  \"hidden_size\": 4096,\n",
       "  \"initializer_range\": 0.02,\n",
       "  \"intermediate_size\": 11008,\n",
       "  \"max_position_embeddings\": 32768,\n",
       "  \"model_type\": \"llama\",\n",
       "  \"num_attention_heads\": 32,\n",
       "  \"num_hidden_layers\": 4,\n",
       "  \"num_key_value_heads\": 32,\n",
       "  \"pad_token_id\": 0,\n",
       "  \"pretraining_tp\": 1,\n",
       "  \"rms_norm_eps\": 1e-05,\n",
       "  \"rope_scaling\": null,\n",
       "  \"tie_word_embeddings\": false,\n",
       "  \"torch_dtype\": \"float16\",\n",
       "  \"transformers_version\": \"4.31.0\",\n",
       "  \"use_cache\": true,\n",
       "  \"vocab_size\": 32000\n",
       "}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ede0e9ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('./out-1b/tokenizer_config.json',\n",
       " './out-1b/special_tokens_map.json',\n",
       " './out-1b/tokenizer.json')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.save_pretrained('./out-1b', safe_serialization=True)\n",
    "tokenizer.save_pretrained('./out-1b', safe_serialization=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b31b26b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 2.0G\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu  628 Sep 19 12:53 config.json\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu  132 Sep 19 12:53 generation_config.json\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu 2.0G Sep 19 12:53 model.safetensors\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu  414 Sep 19 12:53 special_tokens_map.json\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu 1.8M Sep 19 12:53 tokenizer.json\r\n",
      "-rw-r--r-- 1 ubuntu ubuntu  725 Sep 19 12:53 tokenizer_config.json\r\n"
     ]
    }
   ],
   "source": [
    "!ls -lh out-1b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5aaf5385",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading package lists... Done\n",
      "Building dependency tree... Done\n",
      "Reading state information... Done\n",
      "sshpass is already the newest version (1.09-1).\n",
      "0 upgraded, 0 newly installed, 0 to remove and 34 not upgraded.\n",
      "\u001b[1;33mW: \u001b[0mTarget Packages (main/binary-amd64/Packages) is configured multiple times in /etc/apt/sources.list.d/google-chrome.list:3 and /etc/apt/sources.list.d/google.list:1\u001b[0m\n",
      "\u001b[1;33mW: \u001b[0mTarget Packages (main/binary-all/Packages) is configured multiple times in /etc/apt/sources.list.d/google-chrome.list:3 and /etc/apt/sources.list.d/google.list:1\u001b[0m\n",
      "# Host jupyter-serve found: line 5\n",
      "/home/ubuntu/.ssh/known_hosts updated.\n",
      "Original contents retained as /home/ubuntu/.ssh/known_hosts.old\n",
      "Warning: Permanently added 'jupyter-serve' (ED25519) to the list of known hosts.\n",
      "Connected to jupyter-serve.\n",
      "sftp> put -r out-1b .\n",
      "Uploading out-1b/ to /home/ubuntu/./out-1b\n",
      "Entering out-1b/\n"
     ]
    }
   ],
   "source": [
    "!sudo apt install sshpass -y\n",
    "!ssh-keygen -f \"/home/ubuntu/.ssh/known_hosts\" -R \"jupyter-serve\"\n",
    "!echo 'put -r out-1b .' | sshpass -p ubuntu123 sftp -o StrictHostKeyChecking=no ubuntu@jupyter-serve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d84665c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/transformers/utils/hub.py:652: UserWarning: The `organization` argument is deprecated and will be removed in v5 of Transformers. Set your organization directly in the `repo_id` passed instead (`repo_id={organization}/{model_id}`).\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d59cc60f35fd45d7ae91ffeed64b865e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/2.14G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/llama-1b-hf-32768-fpf/commit/f4adf91594daa73a1cf057d4dbfd3b8381b01113', commit_message='Upload LlamaForCausalLM', commit_description='', oid='f4adf91594daa73a1cf057d4dbfd3b8381b01113', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub('llama-1b-hf-32768-fpf', organization='mesolitica', safe_serialization=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bebc974c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/llama-1b-hf-32768-fpf/commit/5d1e1b67a6dfa1794624d7248004c14f95466687', commit_message='Upload tokenizer', commit_description='', oid='5d1e1b67a6dfa1794624d7248004c14f95466687', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.push_to_hub('llama-1b-hf-32768-fpf', organization='mesolitica')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8893abe4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
